<?php
/**
 * 
 * 导航菜单数据提供器
 * @author LeoNing
 *
 */
class NaviDataProvider implements IDataProvider {
	/*
	 * (non-PHPdoc) @see IDataProvider::provide()
	 */
	public function provide($params) {
		global $_PAGE;
		extract ( $params );
		$where = array ('NI.up_id' => 0 );
		$miM = new WebMenuItemModel ();
		$miM->alias ( 'NI' );
		if (isset ( $parent )) { //读取子分类
			$upid = $parent ['id'];
			$where ['NI.up_id'] = $upid;
		} else if (isset ( $name )) { //读取批定名称的导航
			$where ['NI.menu_name'] = $name;
		} else { //读取默认导航
			$where ['NN.menu_default'] = 1;
			$miM->join ( 'web_menu AS NN', "NI.menu_name = NN.menu_name" );
		}
		$miM->join ( "web_page AS WP", "NI.page_id = WP.page_id" );
		$miM->join ( "web_page_category AS WPC", "WP.page_id = WPC.page_id" );
		$miM->sortAsc ( 'sort' );
		$items = $miM->where ( $where )->retrieve ( 'NI.menuitem_id as id,NI.item_name as name,NI.title,NI.url,NI.target,NI.page_id,WP.url as url1,WPC.bind,WPC.home as domain_home' );
		$menus = array ();
		if ($items) {
			foreach ( $items as $item ) {
				$item ['url'] = empty ( $item ['url'] ) ? $item ['url1'] : $item ['url'];
				$item ['subactived'] = $this->isActived ( $item ['page_id'], $_PAGE ['subcrumbs'] );
				if ($item ['page_id'] == $_PAGE ['page_id']) {
					$item ['actived'] = true;
				} else {
					$item ['actived'] = $this->isActived ( $item ['page_id'], $_PAGE ['crumbs'] );
				}
				$menus [] = $item;
			}
		}
		return new CtsDataSet ( $menus );
	}
	protected  function isActived($page_id, $crumbs) {
		if (! empty ( $crumbs )) {
			foreach ( $crumbs as $c ) {
				if ($c ['page_id'] == $page_id && $c ['page_id']) {
					return true;
				}
			}
		}
		return false;
	}
}